﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Data.Entity.ModelConfiguration;

namespace DataModel
{
    public class Book
    {
        [Column("ID")]
        public int ID { get; set;  }
        [Column("TITLE")]
        public string Title { get; set; }
        [Column("PUBLISHER_ID")]
        public int? Publisher_ID { get; set; }
        [Column("BINDING")]
        public string Binding { get; set; }
        [Column("ISBN")]
        public string ISBN { get; set; }
        [Column("ASIN")]
        public string ASIN { get; set; }
        [Column("IMAGE")]
        public string Image { get; set; }
        [Column("NUMBEROFPAGES")]
        public int? NumberOfPages { get; set; }
        [Column("PUBLICATIONDATE")]
        public string PublicationDate { get; set; }
        [Column("DETAILPAGEURL")]
        public string DetailPageUrl { get; set; }
        [Column("LANGUAGEORIGINAL")]
        public string LanguageOriginal { get; set; }
        [Column("LANGUAGEPUBLISHED")]
        public string LanguagePublished { get; set; }
        [Column("SUBJECT")]
        public string Subject { get; set; }
        [Column("CLASS")]
        public int Class { get; set; }

        public virtual List<Author> Authors { get; set; }
        public virtual Publisher Publisher { get; set; }
        public virtual List<BookCopy> BookCopies { get; set; }

        public bool Exists()
        {
            DataLayer data = DataLayer.Data;

            bool found = false;
            if (ISBN != "")
                found = data.Books.Where(x => x.ISBN == ISBN).Count() > 0;

            return found;
        }
    }

    public class BookConfiguration : EntityTypeConfiguration<Book>
    {
        public BookConfiguration()
        {
            HasKey(x => x.ID);
            Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            HasOptional(b => b.Publisher).WithMany(p => p.Books).HasForeignKey(b => b.Publisher_ID);
            HasMany(x => x.Authors)
                .WithMany(x => x.Books)
                .Map(x =>
                {
                    x.MapLeftKey("BOOK_ID");
                    x.MapRightKey("AUTHOR_ID");
                    x.ToTable("BOOK_AUTHOR");
                });
            ToTable("BOOK");
        }
    }
}
